<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryAmount } from '@/api/business/largeScreen'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '270px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart('14')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryAmount({ zhlb: zhlb }).then(res => {
        console.log('queryAmount:{}', res)
        this.chart.setOption({
          title: [
            { text: '每月统筹支出', textStyle: { color: '#fff', fontSize: '14' }}
          ],
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: '17%',
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: res.monthArray,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              fontWeight: 'bold',
              textShadowColor: '#000',
              textShadowOffsetY: 2
            }
          }],
          yAxis: [
            {
              type: 'value',
              axisLine: {
                show: true,
                lineStyle: {
                  width: 2,
                  color: 'rgba(255,255,255,.6)'
                }
              },
              axisLabel: {
                color: '#fff',
                fontSize: 12,
                textShadowColor: '#000',
                textShadowOffsetY: 2
              }
            }],
          series: [{
            name: '职工统筹支出',
            type: 'bar',
            data: res.zgArray,
            color: ['#87CEFA']
          }, {
            name: '居民统筹支出',
            type: 'bar',
            data: res.jmArray,
            color: ['#3a91d2']
          }]
        })
      })
    }
  }
}
</script>
